package cn.com.controller.manageController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Taste;
import cn.com.db.service.TasteService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@Auth(UserAuth.ADMIN)
@RequestMapping("/back/taste")
@RestController
public class TasteManageController {

    @Autowired
    TasteService tasteService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String createTaste(Taste taste){
        DataResult dataResult=new DataResult();
        taste.setId(UidUtil.createUUID());
        taste.setCreated_at(TimeUtil.getNowTimestamp());
        taste.setUpdated_at(TimeUtil.getNowTimestamp());
        tasteService.createTaste(taste);
        dataResult.setData(taste);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("添加成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public String viewTaste(@PathVariable("id")String id){
        DataResult dataResult=new DataResult();
        if (!tasteService.isExist(id)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("口味不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setData(tasteService.getTasteById(id));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("获取成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getTaste(){
        DataResult dataResult=new DataResult();
        dataResult.setTotal(tasteService.countTaste());
        dataResult.setData(tasteService.getTaste());
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Taste taste){
        DataResult dataResult=new DataResult();
        if (!tasteService.isExist(taste.getId())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("口味不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        tasteService.updateTaste(taste);
        dataResult.setData(tasteService.getTasteById(taste.getId()));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("编辑成功");
        return JsonUtil.toJsonString(dataResult);
    }



}
